#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<assert.h>
#define MIN_NUMBER 1
#define MAX_NUMBER 100
#define MAX_TIMES 10
char MenuSelection(void);
void GuessOnce(int magic);
void GuessUntilRight(int magic);
void GuessUpToTen(int magic);
int MakeNumber(void);
int IsRight(int magic, int guess);

int IsValidNum(int number)
{
	if (number >= MIN_NUMBER && number <= MAX_NUMBER)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int IsRight(int magic, int guess)
{
	if (guess < magic)
	{
		printf("Wrong!Too small!\n");
		return 0;
	}
	else if (guess > magic)
	{
		printf("Wrong!Too big!\n");
		return 0;
	}
	else
	{
		return 1;
	}

}
int MakeNumber(void)
{
	int magic;
	srand(time(NULL));
	magic = (rand() % (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER;
	return magic;
}
char MenuSelection(void)
{
	char choice;
	printf("1.Guess Once\n");
	printf("2.Guess until right\n");
	printf("3.Guess up to ten times\n");
	printf("0.Exit\n");
	printf("Input your choice:");
	scanf_s("%c",&choice);
	return choice;
}

int InputGuess(int counter)
{
	int guess=0;
	int ret;
	printf("Try %d", counter + 1);
	ret = scanf_s("%d", &guess);
	while (ret != 1 || !IsValidNum(guess))
	{
		printf("Input error!\n");
		while (getchar() != '\n');
		printf("Try %d:", counter + 1);
		ret = scanf_s("%d", &guess);
	}
	return guess;
}
void GuessOnce(const int magic)
{
	int guess;
	int counter = 0;
	guess = InputGuess(counter);
	if (IsRight(magic, guess))
	{
		printf("Right!\n");

	}
	printf("The magic umber is %d\n", magic);
}
void GuessUntilRight(const int magic)
{
	int guess;
	int counter = 0;
	do
	{
		guess = InputGuess(counter);
		counter++;

	} while (!IsRight(magic, guess));
	printf("The magic number is % d\n",magic);
	printf("counter=%d\n",counter);
}
void GuessUpToTen(const int magic)
{
	int guess;
	int counter = 0;
	do
	{
		guess = InputGuess(counter);
		counter++;
	} while (!IsRight(magic, guess) && counter < MAX_TIMES);
		printf("The magic number is %d\n", magic);
	printf("counter=%d\n", counter);
}
int main(void)
{
	int magic;
	char choice;
	do
	{
		magic = MakeNumber();
		choice = MenuSelection();
		switch (choice)
		{
		case'1':
			GuessOnce(magic);
			break;
		case'2':
			GuessUntilRight(magic);
			break;
		case'3':
			GuessUpToTen(magic);
			break;
		default:
			printf("Input error!\n");
			while (getchar() != '\n');
		}
	} while (choice != '0');
	printf("Game is over!\n");
	return 0;
}

